//
//  PBNaviTitleView.mm
//  PlatinumButler
//
//  Created by 高小松 on 14-3-24.
//  Copyright (c) 2014年 gxsoft. All rights reserved.
//

#import "PBNaviTitleView.h"
#import "PBRect.h"

@interface PBNaviTitleView ()

@property (nonatomic, retain) UILabel * textLabel;      // 标题视图
@property (nonatomic, retain) UIButton * leftButton;    // 左侧按钮视图
@property (nonatomic, retain) UIButton * rightButton;   // 右侧按钮视图

@end

@implementation PBNaviTitleView

#pragma mark - Memory Management

- (id)init
{
    self = [super init];
    if (self) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_textLabel];
    }
    return self;
}

- (void)dealloc
{
    [_textLabel release];
    [_leftButton release];
    [_rightButton release];
    [super dealloc];
}

#pragma mark - View Management

- (void)layoutSubviews
{
    static CGFloat margin = 2;
    CGRect rect = self.bounds;
    if (_leftButton != nil) {
        CGRect rcLeft = LeftCenterRect(rect, CGRectGetWidth(_leftButton.bounds), CGRectGetHeight(_leftButton.bounds), margin);
        _leftButton.frame = rcLeft;
        DeflateRect(&rect, margin + CGRectGetWidth(_leftButton.bounds), 0, 0, 0);
    }
    if (_rightButton != nil) {
        CGRect rcRight = RightCenterRect(rect, CGRectGetWidth(_rightButton.bounds), CGRectGetHeight(_rightButton.bounds), margin);
        _rightButton.frame = rcRight;
        DeflateRect(&rect, 0, 0, margin + CGRectGetWidth(_rightButton.bounds), 0);
    }
    _textLabel.frame = rect;
}

#pragma mark - Components

- (void)setTitle:(NSString *)text
{
    _textLabel.text = text;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textLabel.textColor = textColor;
}

- (void)addLeftButton:(UIButton *)left target:(id)target selector:(SEL)selector
{
    self.leftButton = left;
    [self.leftButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [self setNeedsDisplay];
}

- (void)addRightButton:(UIButton *)right target:(id)target selector:(SEL)selector
{
    self.rightButton = right;
    [self.rightButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [self setNeedsDisplay];
}

@end
